package com.hzyl.famousreader.view.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzyl.famousreader.Constant;
import com.hzyl.famousreader.R;
import com.hzyl.famousreader.ThemeColor;
import com.hzyl.famousreader.router.Router;
import com.hzyl.famousreader.view.adapter.ThemeAdapter;
import com.hzyl.famousreader.view.fragment.UserFragment;
import com.hzyl.famousreader.databinding.*;
import java.util.Arrays;
import java.util.List;

@Route(path = Router.ACTIVITY_THEME)
public class ThemeActivity extends BaseActivity {

  @Autowired
  UserFragment userFragment;

  private ActivityThemeBinding binding;

  //private MainViewModel mainViewModel;

  private boolean isChangeTheme = false;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ARouter.getInstance().inject(this);

    binding = DataBindingUtil.setContentView(this, R.layout.activity_theme);

    //mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);

    int colorValue = SPUtils.getInstance().getInt(Constant.SP_THEME_COLOR,  ThemeColor.DEFAULT.getColor());
    BarUtils.setStatusBarColor(this, colorValue, 0);
    binding.toolbar.setBackgroundColor(colorValue);

    List<ThemeColor> colorList = Arrays.asList(ThemeColor.values());
    ThemeAdapter adapter = new ThemeAdapter(colorList);
    adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
      @Override
      public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        isChangeTheme = true;
        ThemeColor themeColor = (ThemeColor) adapter.getData().get(position);

        //保存颜色值
        SPUtils.getInstance().put(Constant.SP_THEME_COLOR, themeColor.getColor());

        //mainViewModel.themeColor.setValue(themeColor.getColor());
        //ActivityUtils.startActivity(ThemeActivity.class);

        //设置当前的显示
        BarUtils.setStatusBarColor(ThemeActivity.this, themeColor.getColor(), 0);
        binding.toolbar.setBackgroundColor(themeColor.getColor());
      }
    });
    binding.themeRvColor.setLayoutManager(new GridLayoutManager(this, 3));
    binding.themeRvColor.setAdapter(adapter);
  }

  @Override
  public void onBackPressed() {
    super.onBackPressed();
    if(isChangeTheme) {
      ActivityUtils.startActivity(MainActivity.class);
    }
  }
}
